import Foundation

enum HomeComponent: Identifiable, Decodable {
    private enum CodingKeys: String, CodingKey {
        case type
        case title
        case items
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let type = try container.decode(String.self, forKey: .type)
        
        switch type {
        case "banner":
            let title = try container.decode(String.self, forKey: .title)
            self = .banner(title: title)
        case "quickEntry":
            let items = try container.decode([QuickEntryItem].self, forKey: .items)
            self = .quickEntry(items: items)
        case "promotion":
            let title = try container.decode(String.self, forKey: .title)
            self = .promotion(title: title)
        default:
            throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Invalid component type")
        }
    }
    case banner(title: String)
    case quickEntry(items: [QuickEntryItem])
    case promotion(title: String)
    
    var id: String {
        switch self {
        case .banner(let title): return "banner_\(title)"
        case .quickEntry: return "quickEntry"
        case .promotion(let title): return "promotion_\(title)"
        }
    }
}

struct QuickEntryItem: Identifiable, Decodable {
    var id = UUID()
    let title: String
    let iconName: String
}

